
import {getLoginUser} from "../../../service/user";
import list_behavior from "../../../behavior/list_behavior";
import {simplePost} from "../../../utils/request";
import {showToast, showToastSuccess} from "../../../utils/tool";
import {emit} from "../../../utils/event";

Page({
    behaviors:[list_behavior],
    data: {
        nav:{
            title:'我的地址'
        },
    },
    onLoad: function (options) {
        (async () => {
            this.setData({
                options:options||{}
            })
        })()
    },
    onShow() {
        this.setData({
            [`list.url`]:'/userAddress/paginate',
        })
        this.setList(1)
    },
    searchSub(e){
        const value = e.detail.value
        this.setData({
            [`list.where.key`]: value
        })
        this.setList(1)
    },

    /**
     * 执行选择地址监听（来自付款页面活着其他需要选择地址的页面）
     * @param e
     */
    selectAddress(e){
        const {index} = e.currentTarget.dataset
        const {list} = this.data
        const item = list.items[index]

        // 向上级页面传递数据
        emit(this,"selectUserAddressEvent", {address: item})
        // 返回上一页
        wx.navigateBack()
    },

    /**
     * 设为默认地址
     */
    setDefault(e){
        const {list} = this.data
        const {index} = e.currentTarget.dataset
        const item = list.items[index]
        item.default = 1
        simplePost("/userAddress/setDefault",{
            id:item.id,
        }).then(r => {
            showToastSuccess("设置成功")
            // 修改页面数据
            const newItems = list.items.map((item,oi) => {
                if (index == oi){
                    item.is_default = 1
                }else{
                    item.is_default = 0
                }
                return item
            })
            this.setData({
                [`list.items`]: newItems,
            })
        })
    },
    /**
     * 编辑
     */
    toEdit(e){
        const {index}= e.currentTarget.dataset
        const {list} = this.data
        const item = (typeof index !== 'undefined') ? list.items[index] : null
        let url = '/pages/user/address/edit'
        if (item){
            url = url + `?id=${item.id}`
        }
        wx.navigateTo({
            url: url,
            events: {
                // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
                editAddressEvent: ({address}) => {
                    if (item){  // **重要**：这里不要判断index，因为index可能是0
                        //  编辑
                        this.setData({
                            [`list.items[${index}]`] : address,
                        })
                    }else{  // 添加
                        // 向列表顶部追加
                        list.items.unshift(address)
                        this.setData({
                            [`list.items`]: list.items
                        })
                    }
                },
            },
        })
    },
    /**
     * 删除地址
     */
    del_address: function (e) {
        console.log("eeeefea", e)
        const {index} = e.currentTarget.dataset
        const {list} = this.data;
        const id = list.items[index].id;
        const self = this
        simplePost('/userAddress/del', {ids:[id]}).then(r => {
            showToast("删除成功")
            self.setData({
                [`list.items[${index}].remove`]:true,
            })
        })

    },
});
